Keith Packard: DRM-lease
DRM display resource leasing (kernel side)
So, you've got a fine head-mounted display and want to explore the
delights of virtual reality. Right now, on Linux, that means getting
the window system to cooperate because the window system is the DRM
master and holds sole access to all display resources. So, you
plug in your device, play with RandR to get it displaying bits from
the window system and then carefully configure your VR application to
use the whole monitor area and hope that the desktop will actually
grant you the boon of page flipping so that you will get reasonable
performance and maybe not even experience tearing. Results so far have
been mixed, and depend on a lot of pieces working in ways that aren't
exactly how they were designed to work.
We could just hack up the window system(s) and try to let applications
reserve the HMD monitors and somehow removing them from the normal
display area so that other applications don't randomly pop up in the
middle of the screen. That would probably work, and would take
advantage of much of the existing window system infrastructure for
setting video modes and performing page flips. However, we've got a
pretty spiffy standard API in the kernel for both of those, and
getting the window system entirely out of the way seems like something
worth trying.
I spent a few hours in Hobart chatting with Dave Airlie during LCA and
discussed how this might actually work.
Goals
- Use KMS interfaces directly from the VR application to drive presentation to the HMD.
- Make sure the window system clients never see the HMD as a connected monitor.
- Maybe let logind (or other service) manage the KMS resources and hand them out to the window system and VR applications.
- Don't make KMS resources appear and disappear. It turns out applications get confused when the set of available CRTCs, connectors and encoders changes at runtime.
- Pretend that connectors were always disconnected
- Mask off crtc and encoder bits so that some of them just didn't seem very useful.
- Block access to resources controlled by other DRM masters, just in case someone tried to do the wrong thing.
- DRM Master
- Any DRM file which can perform mode setting.
- Owner
- The original DRM Master, created by opening /dev/dri/card*
- Lessor
- A DRM master which has leased out resources to one or more other DRM masters.
- Lessee
- A DRM master which controls resources leased from another DRM master. Each Lessee leases resources from a single Lessor.
- Lessee ID
- An integer which uniquely identifies a lessee within the tree of DRM masters descending from a single Owner.
- Lease
- The contract between the Lessor and Lessee which identifies which resources which may be controlled by the Lessee. All of the resources must be owned by or leased to the Lessor.
int drmModeCreateLease(int fd,
const uint32_t *objects,
int num_objects,
int flags,
uint32_t *lessee_id);
Given an FD to a DRM master, and a list of objects to lease, a new DRM
master FD is returned that holds a lease to those objects. 'flags'
can be any combination of O_CLOEXEC and O_NONBLOCK for the newly
minted file descriptor.
Of course, the owner might want to take some resources back, or even
grant new resources to the lessee. So, I added an interface that
rewrites the terms of the lease with a new set of objects:
int drmModeChangeLease(int fd,
uint32_t lessee_id,
const uint32_t *objects,
int num_objects);
Note that nothing here makes any promises about the state of the
objects across changes in the lease status; the lessor and lessee are
expected to perform whatever modesetting is required for the objects
to be useful to them.
Window System Integration
There are two ways to integrate DRM leases into the window system
environment:
- Have logind "lease" most resources to the window system. When a HMD is connected, it would lease out suitable resources to the VR environment.
- Have the window system "own" all of the resources and then add window system interfaces to create new DRM masters leased from its DRM master.
- What should happen when a Lessor is closed? Should all access to controlled resources be revoked from all descendant Lessees? Proposed answer -- lessees hold a reference to their lessor so that the entire tree remains in place. A Lessor can clean up before exiting by revoking lessee access if it chooses.
- How about when a Lessee is closed? Should the Lessor be notified in some way?
- CRTCs and Encoders have properties. Should these properties be automatically included in the lease? Proposed answer -- no, userspace is responsible for constructing the entire lease.